package com.aide.simplification.global.filter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.aide.simplification.global.Utils;
import com.aide.simplification.popedom.login.LoginUser;
import com.aide.simplification.popedom.login.PopSSO;
import com.aide.simplification.popedom.popedom.base.PopedomOP;

/**
 * dwr access control
 * @author sorc
 *
 */
public class DwrACL extends HttpServlet implements Filter {
	private static final long serialVersionUID = 4336997654968774762L;
	private FilterConfig filterConfig;
	public void init(FilterConfig filterConfig) throws ServletException {
		this.filterConfig = filterConfig;
	}
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain filterChain) {
		RequestDispatcher dispatcher = null;
		ServletContext context = filterConfig.getServletContext();
		
		try {
			HttpServletRequest httpServletRequest = (HttpServletRequest) request;
			HttpServletResponse httpServletResponse = (HttpServletResponse) response;
			//HttpSession session = httpServletRequest.getSession();
			//String path = httpServletRequest.getContextPath();
			PopSSO popsso= new PopSSO(httpServletRequest,httpServletResponse);
			LoginUser loginUser= new LoginUser();
			loginUser=popsso.ssoSync();
			PopedomOP pp = new PopedomOP();
			if (loginUser != null && pp.checkPopAnd(Utils.getConfig("pop.dwrpop"), loginUser.getPop())) {
				filterChain.doFilter(request, response);
			} else {
				dispatcher = context
						.getRequestDispatcher(Utils.getConfig("pop.path")+Utils.getConfig("pop.login_page")+"?url="+httpServletRequest.getRequestURI());
				dispatcher.forward(request, response);
			}
		} catch (Exception iox) {
			iox.printStackTrace();
			filterConfig.getServletContext().log(iox.getMessage());
		}
	}
	public void destroy() {
	}
}
